package com.learning.jquizenglish.screen.profile;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.common.adapter.BaseListAdapter;
import com.learning.jquizenglish.model.ProfileModel;
import com.learning.jquizenglish.model.UserRowModel;
import com.learning.jquizenglish.model.UserRowModel.ProfileAction;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.widget.ViewHolder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ProfileAdapter extends BaseListAdapter<UserRowModel>{

	public static final int AVATAR_TYPE = 0;
	public static final int HEADER_TYPE = 1;
	public static final int USER_INFO_TYPE = 2;
	public static final int USER_ACTIVITY_TYPE = 3;
	private ProfileScreen mFrag;
	
	public ProfileAdapter(Activity activity, ArrayList<UserRowModel> datas) {
		super(activity, datas);
	}
	
	public ProfileAdapter(ProfileScreen frag, ArrayList<UserRowModel> datas) {
		super(frag.getActivity(), datas);
		mFrag = frag;
	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}
	
	@Override
	public int getItemViewType(int position) {
		switch (getItem(position).rowType) {
		case AVATAR:
			return AVATAR_TYPE;
		case HEADER:
			return HEADER_TYPE;
		case USER_INFO:
			return USER_INFO_TYPE;
		case USER_ACTIVITY:
			return USER_ACTIVITY_TYPE;
		default:
			return USER_INFO_TYPE;
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		switch (getItemViewType(position)) {
		case AVATAR_TYPE:
			if (convertView==null) {
				convertView = LayoutInflater.from(mActivity).inflate(R.layout.profile_item_avatar, null);
			}
			initAvatarView(convertView, getItem(position));
			break;
		case HEADER_TYPE:
			if (convertView==null) {
				convertView = LayoutInflater.from(mActivity).inflate(R.layout.profile_item_header, null);
			}
			initHeaderView(convertView, getItem(position));
			break;
		case USER_INFO_TYPE:
			if (convertView==null) {
				convertView = LayoutInflater.from(mActivity).inflate(R.layout.profile_item_info, null);
			}
			initUserInfoView(convertView, getItem(position));
			break;
		case USER_ACTIVITY_TYPE:
			if (convertView==null) {
				convertView = LayoutInflater.from(mActivity).inflate(R.layout.profile_item_recent_activity, null);
			}
			initUserActivityView(convertView, getItem(position));
			break;
		default:
			break;
		}
		return convertView;
	}
	
	private void initAvatarView(View convertView,final UserRowModel model){
		final ImageView imgAvatar = ViewHolder.get(convertView, R.id.imgAvatar);
		TextView txtUser = ViewHolder.get(convertView, R.id.txtUserName);
		String avatarLocalUri = ProfilePreference.getInstance().getAvatarUriFromPref();
		if (avatarLocalUri!=null && !avatarLocalUri.equals("")) {
			ImageLoader.getInstance().displayImage(avatarLocalUri, imgAvatar,ProfileScreen.options);
		}else{
			if (model.strFirst!=null && !model.strFirst.equals("")) {
				ImageLoader.getInstance().displayImage(JQuizEnglishApi.BASE_IMAGE_URL+model.strFirst, imgAvatar,ProfileScreen.options);
			}
		}
		txtUser.setText("("+model.strSecond+")");
		imgAvatar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				UserActionExecutor.getInstance().executeAction(imgAvatar, mFrag, model);
			}
		});
	}
	
	private void initHeaderView(View convertView,UserRowModel model){
		TextView txtHeader = ViewHolder.get(convertView, R.id.txtHeader);
		txtHeader.setText(model.strFirst);
	}

	private void initUserInfoView(View convertView,final UserRowModel model){
		TextView txtTitle = ViewHolder.get(convertView, R.id.txtTitle);
		TextView txtContent = ViewHolder.get(convertView, R.id.txtContent);
		TextView txtEdit = ViewHolder.get(convertView, R.id.txtEdit);
		//bind data to view
		txtTitle.setText(model.strFirst);
		txtContent.setText(model.strSecond);
		if(model.action == ProfileAction.NONE){
			txtEdit.setVisibility(View.GONE);
		}else{
			txtEdit.setVisibility(View.VISIBLE);
		}
		txtEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				UserActionExecutor.getInstance().executeAction(v, mFrag, model);
			}
		});
	}
	
	private void initUserActivityView(View convertView,final UserRowModel model){
		TextView txtTime = ViewHolder.get(convertView, R.id.txtTime);
		TextView txtRecentActivity = ViewHolder.get(convertView, R.id.txtRecentActivity);
		TextView txtStatus = ViewHolder.get(convertView, R.id.txtStatus);
		//bind data to view
		txtTime.setText(model.strFirst);
		txtRecentActivity.setText(model.strSecond);
		if (model.action ==  ProfileAction.NONE) {
			txtStatus.setVisibility(View.GONE);
		}else{
			txtStatus.setVisibility(View.VISIBLE);
			txtStatus.setText("(review)");
		}
		txtStatus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				UserActionExecutor.getInstance().executeAction(v, mFrag, model);
			}
		});
	}
	
}
